/*
 * 

 * 
 */
package com.cheche.dn.common;

import com.google.common.collect.Lists;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import java.io.Serializable;
import java.util.List;

/**
 * 分页信息
 * 
 * 
 * @version 1.0
 */
public class Pageable implements Serializable {

	private static final long serialVersionUID = -3930180379790344299L;

	/** 默认页码 */
	private static final int DEFAULT_PAGE_NUMBER = 1;

	/** 默认每页记录数 */
	public static final int DEFAULT_PAGE_SIZE = 20;

	/** 最大每页记录数 */
	private static final int MAX_PAGE_SIZE = 1000;

	private int pageNumber;

	private int pageSize;

	private List<Filter> filters;

	private List<Order> orders;

	public static class Builder {
		/** 页码 */
		private int pageNumber = DEFAULT_PAGE_NUMBER;
		/** 每页记录数 */
		private int pageSize = DEFAULT_PAGE_SIZE;
		/** 筛选 */
		private List<Filter> filters = Lists.newArrayList();
		/** 排序 */
		private List<Order> orders = Lists.newArrayList();

		public Builder() {
		}

		public Builder(int pn, int pz) {
			pageNumber(pn);
			pageSize(pz);
		}

		/**
		 * 设置页码值
		 * @param val
		 * 			页码
		 * @return builder
		 */
		public Builder pageNumber(int val) {
			if (val >= 1) {
				this.pageNumber = val;
			}
			return this;
		}

		/**
		 * 设置每页数量
		 * @param val
		 * 			数量
		 * @return builder
		 */
		public Builder pageSize(int val) {
			if (val >= 1 && val <= MAX_PAGE_SIZE) {
				this.pageSize = val;
			}
			return this;
		}

		/**
		 * 追加一个排序属性
		 * @param order
		 * 			排序属性
		 * @return builder
		 */
		public Builder addOrder(Order order) {
			this.orders.add(order);
			return this;
		}

		/**
		 * 追加一组排序属性
		 * @param orders
		 * 			一组排序属性
		 * @return builder
		 */
		public Builder addOrders(List<Order> orders) {
			this.orders.addAll(orders);
			return this;
		}

		/**
		 * 设置排序属性
		 * @param orders
		 * 			一组排序属性
		 * @return builder
		 */
		public Builder orders(List<Order> orders) {
			this.orders = orders;
			return this;
		}

		/**
		 * 追加一个筛选
		 * @param filter
		 * 			筛选
		 * @return builder
		 */
		public Builder addFilter(Filter filter) {
			this.filters.add(filter);
			return this;
		}

		/**
		 * 追加一组筛选
		 * @param filters
		 * 			一组筛选
		 * @return builder
		 */
		public Builder addFilters(List<Filter> filters) {
			this.filters.addAll(filters);
			return this;
		}

		/**
		 * 设置筛选
		 * @param filters
		 * 			一组筛选
		 * @return builder
		 */
		public Builder filters(List<Filter> filters) {
			this.filters = filters;
			return this;
		}

		/**
		 * 构建一个新的Pageable对象
		 */
		public Pageable build() {
			return new Pageable(this);
		}
	}

	private Pageable(Builder builder) {
		pageNumber = builder.pageNumber;
		pageSize = builder.pageSize;
		filters = builder.filters;
		orders = builder.orders;
	}

	/**
	 * 获取页码
	 * 
	 * @return 页码
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * 获取每页记录数
	 * 
	 * @return 每页记录数
	 */
	public int getPageSize() {
		return pageSize;
	}

    /**
     * 获取排序
     *
     * @return 排序
     */
    public List<Order> getOrders() {
        return orders;
    }

	/**
	 * 获取筛选
	 * 
	 * @return 筛选
	 */
	public List<Filter> getFilters() {
		return filters;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		Pageable other = (Pageable) obj;
		return new EqualsBuilder().append(getPageNumber(), other.getPageNumber()).append(getPageSize(),
                other.getPageSize()).append(getFilters(), other.getFilters()).append(getOrders(), other.getOrders()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).append(getPageNumber()).append(getPageSize()).append(getFilters()).append(getOrders()).toHashCode();
	}

}